<template>
  <div class="main">
    <!--搜索部分-->
    <div class="user-search-container">



      <el-form ref="params" :inline="true" v-show="true" label-width="68px">

        <el-form-item label="类型" prop="policyType">
          <el-select v-model="params.policyType" placeholder="类型" clearable size="small" class="item-input">
            <el-option v-for="dict in ORDER_TYPE" :key="dict.value" :label="dict.text" :value="dict.value" />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="cyan" icon="el-icon-search" size="mini" @click="queryList(true)">搜索</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="cyan" icon="el-icon-plus" size="mini" @click="showInit = true">初始化</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="cyan" icon="el-icon-plus" size="mini" @click="showAdd = true">添加</el-button>
        </el-form-item>
      </el-form>

    </div>

    <!--列表部分-->
    <div class="user-list-container">

      <van-divider />

      <el-table v-loading="isLoading" :data="upgradeList">
        <el-table-column label="类型" align="center" prop="policyType"/>
        <el-table-column label="时间(月)" align="center" prop="policyGrade"/>
        <el-table-column label="金额" align="center" >
          <template slot-scope="scope">
            <el-input v-model="scope.row.policyContent" placeholder="缴费金额" clearable size="small"/>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime"/>
        <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="updatePolicy(scope.row)">保存</el-button>

          </template>
        </el-table-column>
      </el-table>
      <van-divider />

    </div>
    <el-dialog title="初始化" :visible.sync="showInit">
      <el-form >

        <el-form-item label="1个月" >
          <el-input v-model="initParams.month1" placeholder="请输入金额"></el-input>
        </el-form-item>
        <el-form-item label="3个月" >
          <el-input v-model="initParams.month3" placeholder="请输入金额"></el-input>
        </el-form-item>
        <el-form-item label="6个月" >
          <el-input v-model="initParams.month6" placeholder="请输入金额"></el-input>
        </el-form-item>
        <el-form-item label="12个月" >
          <el-input v-model="initParams.month12" placeholder="请输入金额"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showInit = false">取 消</el-button>
        <el-button type="primary" @click="init(initParams)">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="添加" :visible.sync="showAdd">
      <el-form >

        <el-form-item label="时间(月)" >
          <el-input v-model="addParams.time" placeholder="请输入金额"></el-input>
        </el-form-item>
        <el-form-item label="金额" >
          <el-input v-model="addParams.amount" placeholder="请输入金额"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showAdd = false">取 消</el-button>
        <el-button type="primary" @click="add(addParams)">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>

  import {ORDER_TYPE, SUCCESS} from "../../../comm/constant";
  import {policyList,policyInit,policyAdd,policyUpdate} from "../../../api/friend";
  import commentUtil from "../../../utils/commentUtil";

  export default {


    data() {
      return {
        //添加参数
        addParams:{
          time: null,
          amount: null,
        },
        //初始化参数
        initParams:{
          month1: null,
          month3: null,
          month6: null,
          month12: null,
        },
        //列表查询参数
        params:{
          policyType: 'UPGRADE',
        },
        ORDER_TYPE:ORDER_TYPE,
        upgradeList: [],
        isLoading: false,
        showAdd: false,
        showInit: false,

      }
    },
    methods: {
      updatePolicy(params){
        policyUpdate(params).then(res =>{
          if(res.code!==SUCCESS){
            this.$notify({ type: 'danger', message: res.msg });
            return;
          }
          commentUtil.showAlertDialog(this,"操作成功!")
        })
      },

      queryList(){//
        if(this.isLoading) return;
        this.isLoading = true;

        policyList(this.params).then(res => {
          if(res.code!==SUCCESS){
            this.$notify({ type: 'danger', message: res.msg });
            return;
          }
          this.upgradeList = res.data;
          this.isLoading = false;
        });
      },
      onClickLeft() {
        this.$router.go(-1);
      },
      onLoad() {
        console.info("加载数据中 第" + this.page + "页" )
        this.queryList(false)
      },
      init(params) {
        policyInit(params).then(res => {
          if(res.code!==SUCCESS){
            this.$notify({ type: 'danger', message: res.msg });
            return;
          }
          commentUtil.showAlertDialog(this,"操作成功!")
        });
      },
      add(params) {
        policyAdd(params).then(res => {
          if(res.code!==SUCCESS){
            this.$notify({ type: 'danger', message: res.msg });
            return;
          }
          commentUtil.showAlertDialog(this,"操作成功!")
        });
      },
    },
    mounted() {
      this.onLoad();
    },

  }
</script>
<style scoped>


  .user-search-container, .user-list-container{
    margin: 8px 17px;
  }
  .user-search-container .van-cell{
    margin: 8px 0px;
  }

  .user-search-container .van-dropdown-menu{
    height: 40px;
    margin: 8px 0px;
  }
  .user-search-container .van-button{
    height: 85px;
    width: 100px;
    background-color: #eeeeee;
    margin: 8px 0px;
  }
  .user-search-container .van-field, .user-search-container .van-cell{
    border: 1px solid #eeeeee;
  }
</style>
